package com.dev;
import com.fb.FileBrowserPage;
import java.io.File;

public abstract class FileBrowserPageHelper {
    public static void makeSort(FileBrowserPage fbp){
        File[] willshow=fbp.willshow;
        String[] names=fbp.names;
        if(fbp.fileOnBefore){
            fbp.willshowSorted=new int[fbp.willshowLen];
            int willshowSorted[]=fbp.willshowSorted;
            if(fbp.showHidden){
                
                for(int index=0;index<fbp.willshowLen;index++){
                    willshowSorted[index]=index;
                }
            }else{//showHidden==false
                
                for(int index=0,offs=0;;index++){
                    if(index<fbp.willshowLen){
                        if(names[index].charAt(0)!='.'){
                            fbp.willshowSorted[offs]=index;
                            offs++;
                        }
                    }else{
                        fbp.willshowSortedLen=offs;
                        break;
                    }
                }
            }
            int a=0,b=fbp.willshowSortedLen-1;
            int willshowSortedLen=fbp.willshowSortedLen;
            while(true){//let file on before
                if(a<b){
                    int ptr1=willshowSorted[a];
                    int ptr2=willshowSorted[b];
                    if(willshow[ptr1].isFile()){
                        a++;
                        if(willshow[ptr2].isDirectory()){
                            b--;
                        }
                    }else if(willshow[ptr2].isDirectory()){
                        b--;
                    }else{
                        willshowSorted[a]=ptr2;
                        willshowSorted[b]=ptr1;
                        a++;b--;
                    }
                }else if(a==b){
                    int ptr1=willshowSorted[a];
                    if(willshow[ptr1].isFile()){
                        a++;
                    }else b--;
                    break;
                }else{
                    break;
                }
            }
            int sort=fbp.sort;
            /*if(sort==1){//此时a和b的值不要再改动
                String[] names=new String[willshowSortedLen];
                for(int i=0;i<willshowSortedLen;i++){
                    int ptr=willshowSorted[i];
                    names[i]=willshow[ptr].f.getName();
                }
                int l=0,r=b;
                for(int index=0;true;){//字典顺序排列
                    if(index<r){
                        if(names[index].compareTo(names[index+1])>0){
                            String s=names[index];
                            names[index]=names[index+1];
                            names[index+1]=s;

                            int ptr=willshowSorted[index];
                            willshowSorted[index]=willshowSorted[index+1];
                            willshowSorted[index+1]=ptr;
                        }
                        index++;
                    }else{
                        if(--r>l){
                            for(index=r-1;index>=l;index--){
                                if(names[index].compareTo(names[index+1])>0){
                                    int ptr=willshowSorted[index];
                                    willshowSorted[index]=willshowSorted[index+1];
                                    willshowSorted[index+1]=ptr;
                                    String s=names[index];
                                    names[index]=names[index+1];
                                    names[index+1]=s;
                                }
                            }
                            if(++l<r){
                                index=l;
                            }else break;
                        }else{
                            break;
                        }
                    }
                }
                l=a;r=willshowSortedLen-1;
                for(int index=l;true;){
                    if(index<r){
                        if(names[index].compareTo(names[index+1])>0){
                            int ptr=willshowSorted[index];
                            willshowSorted[index]=willshowSorted[index+1];
                            willshowSorted[index+1]=ptr;
                            String s=names[index];
                            names[index]=names[index+1];
                            names[index+1]=s;
                        }
                        index++;
                    }else{
                        if(--r>l){
                            for(index=r-1;index>=l;index--){
                                if(names[index].compareTo(names[index+1])>0){
                                    int ptr=willshowSorted[index];
                                    willshowSorted[index]=willshowSorted[index+1];
                                    willshowSorted[index+1]=ptr;
                                    String s=names[index];
                                    names[index]=names[index+1];
                                    names[index+1]=s;
                                }
                            }
                            if(++l<r){
                                index=l;
                            }else break;
                        }else{
                            break;
                        }
                    }
                }
            }else if(sort==2){
                String[] names=new String[willshowSortedLen];
                for(int i=0;i<willshowSortedLen;i++){
                    int ptr=willshowSorted[i];
                    names[i]=willshow[ptr].f.getName();
                }
                int l=0,r=b;
                for(int index=0;true;index++){//字典逆序排列
                    if(index<r){
                        if(names[index].compareTo(names[index+1])<0){
                            int ptr=willshowSorted[index];
                            willshowSorted[index]=willshowSorted[index+1];
                            willshowSorted[index+1]=ptr;
                            String s=names[index];
                            names[index]=names[index+1];
                            names[index+1]=s;
                        }
                    }else{
                        if(--r>l){
                            for(index=r-1;index>=l;index--){
                                if(names[index].compareTo(names[index+1])<0){
                                    int ptr=willshowSorted[index];
                                    willshowSorted[index]=willshowSorted[index+1];
                                    willshowSorted[index+1]=ptr;
                                    String s=names[index];
                                    names[index]=names[index+1];
                                    names[index+1]=s;
                                }
                            }
                            if(++l<r){
                                index=l-1;
                            }else break;
                        }else{
                            break;
                        }
                    }
                }
                l=a;r=willshowSortedLen-1;
                for(int index=l;true;index++){
                    if(index<r){
                        if(names[index].compareTo(names[index+1])<0){
                            int ptr=willshowSorted[index];
                            willshowSorted[index]=willshowSorted[index+1];
                            willshowSorted[index+1]=ptr;
                            String s=names[index];
                            names[index]=names[index+1];
                            names[index+1]=s;
                        }
                    }else{
                        if(--r>l){
                            for(index=r-1;index>=l;index--){
                                if(names[index].compareTo(names[index+1])<0){
                                    int ptr=willshowSorted[index];
                                    willshowSorted[index]=willshowSorted[index+1];
                                    willshowSorted[index+1]=ptr;
                                    String s=names[index];
                                    names[index]=names[index+1];
                                    names[index+1]=s;
                                }
                            }
                            if(++l<r){
                                index=l-1;
                            }else break;
                        }else{
                            break;
                        }
                    }
                }
            }*/
        }
    }
}
